import pandas as pd
import plotly.express as px
data = pd.read_csv('../data/life-expectancy.csv')
data.head()
| Entity | Code | Year | Life expectancy | |
|---|---|---|---|---|
| 0 | Afghanistan | AFG | 1950 | 27.638 |
| 1 | Afghanistan | AFG | 1951 | 27.878 |
| 2 | Afghanistan | AFG | 1952 | 28.361 |
| 3 | Afghanistan | AFG | 1953 | 28.852 |
| 4 | Afghanistan | AFG | 1954 | 29.350 |
data = data.sort_values(by='Life expectancy', ascending=False)
data.head()
| Entity | Code | Year | Life expectancy | |
|---|---|---|---|---|
| 11005 | Monaco | MCO | 2019 | 86.751 |
| 11004 | Monaco | MCO | 2018 | 86.560 |
| 11003 | Monaco | MCO | 2017 | 86.325 |
| 11002 | Monaco | MCO | 2016 | 86.049 |
| 11001 | Monaco | MCO | 2015 | 85.739 |
fig = px.line(data, x='Year', y='Life expectancy', color='Entity')
fig.show()
asia = data[data['Entity'] == 'Asia']
asia
| Entity | Code | Year | Life expectancy | |
|---|---|---|---|---|
| 1010 | Asia | NaN | 2019 | 73.593 |
| 1009 | Asia | NaN | 2018 | 73.381 |
| 1008 | Asia | NaN | 2017 | 73.150 |
| 1007 | Asia | NaN | 2016 | 72.897 |
| 1006 | Asia | NaN | 2015 | 72.620 |
| ... | ... | ... | ... | ... |
| 941 | Asia | NaN | 1950 | 41.115 |
| 940 | Asia | NaN | 1913 | 28.100 |
| 939 | Asia | NaN | 1900 | 28.000 |
| 938 | Asia | NaN | 1880 | 27.500 |
| 937 | Asia | NaN | 1770 | 27.500 |
74 rows × 4 columns
import plotly.express as px
fig = px.choropleth(asia, locations="Entity",
color="Life expectancy", # lifeExp is a column of gapminder
hover_name="Entity",
animation_frame='Year', # column to add to hover information
color_continuous_scale=px.colors.sequential.Plasma)
fig.update_layout(
autosize=False,
margin = dict(
l=0,
r=0,
b=0,
t=0,
pad=4,
autoexpand=True
),
width=800,
# height=400,
)
for k in range(len(fig.frames)):
fig.frames[k]['layout'].update(title_text=f'My title {k}')
fig.show()
data.columns
Index(['Entity', 'Code', 'Year', 'Life expectancy'], dtype='object')
fig = px.bar(data, y='Life expectancy', x='Year', text='Life expectancy')
fig.update_traces(texttemplate='%{text:.2s}', textposition='outside')
fig.update_layout(uniformtext_minsize=8, uniformtext_mode='hide')
fig.show()
data.Year.unique()
array([2019, 2018, 2017, 2016, 2015, 2014, 2013, 2012, 2011, 2010, 2009,
2008, 2007, 2006, 2005, 2004, 2003, 2002, 2001, 2000, 1999, 1998,
1997, 1996, 1995, 1994, 1993, 1992, 1991, 1990, 1989, 1988, 1987,
1986, 1985, 1984, 1983, 1982, 1981, 1980, 1979, 1978, 1977, 1976,
1975, 1974, 1973, 1972, 1971, 1970, 1969, 1968, 1967, 1966, 1965,
1964, 1963, 1962, 1961, 1960, 1959, 1958, 1957, 1956, 1955, 1954,
1953, 1952, 1951, 1950, 1949, 1948, 1947, 1946, 1942, 1943, 1945,
1944, 1939, 1938, 1941, 1937, 1936, 1940, 1934, 1935, 1933, 1932,
1931, 1930, 1928, 1926, 1925, 1929, 1923, 1924, 1922, 1927, 1921,
1917, 1913, 1920, 1919, 1914, 1909, 1915, 1916, 1910, 1911, 1912,
1905, 1907, 1906, 1902, 1908, 1918, 1904, 1903, 1898, 1901, 1895,
1897, 1896, 1900, 1879, 1892, 1893, 1889, 1888, 1894, 1880, 1878,
1886, 1887, 1854, 1899, 1858, 1891, 1885, 1870, 1884, 1881, 1890,
1855, 1865, 1856, 1857, 1872, 1860, 1859, 1866, 1877, 1851, 1871,
1873, 1883, 1850, 1869, 1864, 1853, 1882, 1852, 1849, 1846, 1867,
1874, 1875, 1861, 1862, 1863, 1868, 1835, 1876, 1845, 1825, 1823,
1841, 1836, 1848, 1824, 1847, 1844, 1840, 1843, 1838, 1826, 1842,
1822, 1583, 1780, 1827, 1833, 1839, 1816, 1578, 1837, 1776, 1828,
1798, 1813, 1573, 1805, 1830, 1818, 1832, 1808, 1774, 1815, 1794,
1797, 1618, 1817, 1787, 1820, 1803, 1802, 1821, 1792, 1753, 1648,
1633, 1831, 1628, 1568, 1760, 1804, 1608, 1793, 1553, 1829, 1775,
1799, 1653, 1773, 1788, 1796, 1548, 1807, 1814, 1603, 1834, 1708,
1703, 1791, 1751, 1767, 1759, 1758, 1777, 1698, 1593, 1766, 1786,
1598, 1781, 1761, 1778, 1754, 1673, 1819, 1755, 1588, 1795, 1713,
1801, 1613, 1563, 1782, 1770, 1748, 1693, 1733, 1643, 1764, 1768,
1756, 1806, 1688, 1765, 1783, 1718, 1723, 1771, 1763, 1752, 1738,
1812, 1769, 1811, 1743, 1638, 1543, 1785, 1757, 1790, 1623, 1800,
1663, 1658, 1779, 1668, 1762, 1678, 1810, 1784, 1683, 1789, 1772,
1809, 1728, 1558])
import plotly.express as px
fig = px.choropleth(data, locations="Code",
color="Life expectancy", # lifeExp is a column of gapminder
hover_name="Entity",
animation_frame='Year', # column to add to hover information
color_continuous_scale=px.colors.sequential.Plasma)
fig.update_layout(
autosize=False,
margin = dict(
l=0,
r=0,
b=0,
t=0,
pad=4,
autoexpand=True
),
width=800,
# height=400,
)
for k in range(len(fig.frames)):
fig.frames[k]['layout'].update(title_text=f'My title {k}')
fig.show()
import plotly.express as px
df = px.data.gapminder().query("year==2007")
fig = px.choropleth(df, locations="iso_alpha",
color="lifeExp", # lifeExp is a column of gapminder
hover_name="country", # column to add to hover information
color_continuous_scale=px.colors.sequential.Plasma)
fig.show()
df.head()
| country | continent | year | lifeExp | pop | gdpPercap | iso_alpha | iso_num | |
|---|---|---|---|---|---|---|---|---|
| 11 | Afghanistan | Asia | 2007 | 43.828 | 31889923 | 974.580338 | AFG | 4 |
| 23 | Albania | Europe | 2007 | 76.423 | 3600523 | 5937.029526 | ALB | 8 |
| 35 | Algeria | Africa | 2007 | 72.301 | 33333216 | 6223.367465 | DZA | 12 |
| 47 | Angola | Africa | 2007 | 42.731 | 12420476 | 4797.231267 | AGO | 24 |
| 59 | Argentina | Americas | 2007 | 75.320 | 40301927 | 12779.379640 | ARG | 32 |